package com.cpfmat.common;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cpfmat.entity.SystemMenu;
import com.cpfmat.entity.SystemParam;
import com.cpfmat.mapper.SystemMenuMapper;
import com.cpfmat.mapper.SystemParamMapper;
import com.cpfmat.vo.Company;
import com.cpfmat.vo.HomeBigPicture;
import com.cpfmat.vo.Partner;
import com.cpfmat.vo.WebSite;
import com.cpfmat.vo.WorkTime;

@Service
public class StartGateServiceData implements ApplicationListener<ContextRefreshedEvent> {
	@Autowired
	private SystemParamMapper systemParamMapper;
	@Autowired
	private SystemMenuMapper systemMenuMapper;

	@SuppressWarnings("unchecked")
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		try {
			if (event.getApplicationContext().getParent() == null) {
				// 从数据库加载网站参数
				if (Contains.company == null || Contains.pictures == null || Contains.webSite == null
						|| Contains.workTime == null || Contains.partners == null) {
					Map<String, Object> paramMap = selectParam();
					
					Contains.company = (Company) paramMap.get("company");
					LogUtils.debug("加载公司参数");
					LogUtils.debug(Contains.company);
					
					Contains.pictures = (List<HomeBigPicture>) paramMap.get("pictures");
					LogUtils.debug("加载首页轮播图参数");
					LogUtils.debug(Contains.pictures);
					
					Contains.webSite = (WebSite) paramMap.get("webSite");
					LogUtils.debug("加载网站参数");
					LogUtils.debug(Contains.webSite);
					
					Contains.workTime = (WorkTime) paramMap.get("workTime");
					LogUtils.debug("加载工作时间参数");
					LogUtils.debug(Contains.workTime);
					
					Contains.partners = (List<Partner>) paramMap.get("partners");
					LogUtils.debug("加载合作伙伴");
					LogUtils.debug(Contains.partners);
				}
				
				if(Contains.menuInfo == null){
					EntityWrapper<SystemMenu> wrapper = new EntityWrapper<SystemMenu>();
					
					wrapper.eq("IS_COLUMN", 1);
					wrapper.eq("DEL_FLAG", 0);
					
					wrapper.orderBy("ORDER_NUM", true);
					
					List<SystemMenu> columns = systemMenuMapper.selectList(wrapper);
					
					Contains.menuInfo = columns;
					
					Map<String,SystemMenu> menuKeyMap = new HashMap<String,SystemMenu>();
					Iterator<SystemMenu> iterator = columns.iterator();
					while(iterator.hasNext()){
						SystemMenu column = iterator.next();
						menuKeyMap.put(column.getKey(), column);
					}
					Contains.menuKeyMap = menuKeyMap;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Map<String, Object> selectParam() {
		EntityWrapper<SystemParam> select = new EntityWrapper<SystemParam>();
		select.eq("DEL_FLAG", 0);
		select.orderBy("PARAM_TYPE,PARAM_KEY");

		List<SystemParam> selectList = systemParamMapper.selectList(select);

		Company company = new Company();
		List<HomeBigPicture> pictures = new ArrayList<HomeBigPicture>();
		WebSite webSite = new WebSite();
		WorkTime workTime = new WorkTime();
		List<Partner> partners = new ArrayList<Partner>();

		Iterator<SystemParam> iterator = selectList.iterator();

		String lastPicturesOrderNumber = "";
		String lastPartnerOrderNumber = "";
		HomeBigPicture lastPicture = null;
		Partner lastPartner = null;

		while (iterator.hasNext()) {
			SystemParam systemParam = iterator.next();
			String type = systemParam.getParamType();
			String key = systemParam.getParamKey();
			String value = systemParam.getParamValue();

			try {
				if ("company".equals(type)) {
					Field field = company.getClass().getDeclaredField(key);
					field.setAccessible(true);
					field.set(company, value);
				} else if ("webSite".equals(type)) {
					Field field = webSite.getClass().getDeclaredField(key);
					field.setAccessible(true);
					field.set(webSite, value);
				} else if ("workTime".equals(type)) {
					Field field = workTime.getClass().getDeclaredField(key);
					field.setAccessible(true);
					field.set(workTime, value);

				} else if ("picture".equals(type)) {
					String[] keys = key.split("-");
					String orderNumber = keys[0];
					key = keys[1];

					if (!lastPicturesOrderNumber.equals(orderNumber)) {
						if (lastPicture != null)
							pictures.add(lastPicture);
						lastPicture = new HomeBigPicture();
						lastPicture.setOrderNumber(orderNumber);
					}
					Field field = lastPicture.getClass().getDeclaredField(key);
					field.setAccessible(true);
					field.set(lastPicture, value);

					lastPicturesOrderNumber = orderNumber;
				} else if ("partner".equals(type)) {
					String[] keys = key.split("-");
					String orderNumber = keys[0];
					key = keys[1];

					if (!lastPartnerOrderNumber.equals(orderNumber)) {
						if (lastPartner != null)
							partners.add(lastPartner);
						lastPartner = new Partner();
						lastPartner.setOrderNumber(orderNumber);
					}
					Field field = lastPartner.getClass().getDeclaredField(key);
					field.setAccessible(true);
					field.set(lastPartner, value);

					lastPartnerOrderNumber = orderNumber;
				}

			} catch (NoSuchFieldException | SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		if(lastPicture != null) pictures.add(lastPicture);
		if(lastPartner != null) partners.add(lastPartner);

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pictures", pictures);
		paramMap.put("company", company);
		paramMap.put("webSite", webSite);
		paramMap.put("workTime", workTime);
		paramMap.put("partners", partners);
		return paramMap;
	}
}
